﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace Deeper.Common
{
	public static class ExtensionMethods
	{
		/// <summary>
		/// 返回该日期是一年中第几周
		/// </summary>
		/// <param name="dt"></param>
		/// <returns></returns>
		public static int WeekOfYear(this DateTime dt)
		{
			DateTime firstday = new DateTime(dt.Year, 1, 1);
			return (dt.DayOfYear + (int)firstday.DayOfWeek - 2) / 7 + 1;
		}


		/// <summary>
		/// 返回某年第几周星期几的日期
		/// </summary>
		/// <param name="dyear"></param>
		/// <param name="dweek"></param>
		/// <param name="dweekday"></param>
		/// <returns></returns>
		public static DateTime DateOfWeek(int dyear, int dweek, int dweekday)
		{
			DateTime firstday = new DateTime(dyear, 1, 1);
			int weekday = (int)firstday.DayOfWeek == 0 ? 7 : (int)firstday.DayOfWeek;
			return firstday.AddDays((dweek - 2) * 7 + weekday + dweekday - 1);
		}

		/// <summary>
		/// 日期转换到字符
		/// </summary>
		/// <param name="date"></param>
		/// <param name="fmt"></param>
		/// <returns></returns>
		public static string ToString(this DateTime? date, string fmt = "yyyy-MM-dd")
		{
			if (date.IsNull()) return "";
			return date.Value.ToString(fmt);
		}



		public static bool IsBetween<T>(this IComparable<T> t, T lowerBound, T upperBound, bool includeLowerBound = false, bool includeUpperBound = false)
		{
			if (t == null) throw new ArgumentNullException("t");

			var lowerCompareResult = t.CompareTo(lowerBound);
			var upperCompareResult = t.CompareTo(upperBound);

			return (includeLowerBound && lowerCompareResult == 0) ||
				(includeUpperBound && upperCompareResult == 0) ||
				(lowerCompareResult > 0 && upperCompareResult < 0);
		}

		/// <summary>
		/// 查找一个对象
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="list"></param>
		/// <param name="predicate"></param>
		/// <returns></returns>
		public static T Find<T>(this IEnumerable<T> list, Func<T, bool> predicate) where T : class
		{
			try
			{
				if (list.IsNull() || list.Count() == 0) return null;
				T t = list.Single(predicate);
				return t;
			}
			catch
			{
				return null;
			}
		}

		/// <summary>
		/// 查找一批对象
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="list"></param>
		/// <param name="predicate"></param>
		/// <returns></returns>
		public static List<T> FindList<T>(this IEnumerable<T> list, Func<T, bool> predicate) where T : class
		{
			if (list.IsNull() || list.Count() == 0) return null;
			try
			{
				return list.Where(predicate).ToList();
			}
			catch { return null; }
		}


		/// <summary>
		/// 检测对象是否为空，为空返回true
		/// </summary>
		public static bool IsNull(this object data)
		{
			return null == data || ((data is string) ? string.IsNullOrEmpty((string)data) : data is DBNull);
		}

		/// <summary>
		/// 判断Datatable是否为空
		/// </summary>
		/// <param name="dt"></param>
		/// <returns></returns>
		public static bool IsNullOrEmpty(this DataTable dt)
		{
			return dt.IsNull() || dt.Rows.Count == 0;
		}
		/// <summary>
		/// 检查对象是否不为空，为空返回false
		/// </summary>
		/// <param name="data"></param>
		/// <returns></returns>
		public static bool IsNotNull(this object data)
		{
			return !data.IsNull();
		}
	}
}
